

package com.aol.expressions.calendar.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This Servlet is used to mock browse calendar handler.
 * 
 * @author 
 * @version 1.0
 */
public class MockBrowseCalendarHandler extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileNotFoundException, URISyntaxException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String view = request.getParameter("view");
            String month = request.getParameter("month");
            String f =request.getParameter("f");
            if (f != null && f.equals("json"))
            {
                if(view.equals("month")&&month.equals("January"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth1.json"));  
                }
                if(view.equals("month")&&month.equals("2"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth2.json"));  
                }
                if(view.equals("month")&&month.equals("3"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth3.json"));  
                }
                if(view.equals("month")&&month.equals("4"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth4.json"));  
                }
                if(view.equals("month")&&month.equals("5"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth5.json"));  
                }
                if(view.equals("month")&&month.equals("June"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth6.json"));  
                }
                if(view.equals("month")&&month.equals("July"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth7.json"));  
                }
                if(view.equals("month")&&month.equals("August"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth8.json"));  
                }
                if(view.equals("month")&&month.equals("9"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth9.json"));  
                }
                if(view.equals("month")&&month.equals("10"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth10.json"));  
                }
                if(view.equals("month")&&month.equals("11"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth11.json"));  
                }
                if(view.equals("month")&&month.equals("December"))
                {
                   out.print(getTextFromFile("/test/calendar/browseCalendarMonth12.json"));  
                }
                if(view.equals("week"))
                {                
                  out.print(getTextFromFile("/test/calendar/browseCalendarWeek.json"));  
                }
            }
        } finally { 
            out.close();
        }
    } 
    
    /**
     * This method is used to get json text from test json file.
     * 
     * @param fileName json file name
     * @return json text
     * @throws java.io.FileNotFoundException if file not found
     * @throws java.net.URISyntaxException if uri syntaax errors
     * @throws java.io.IOException if io error
     */
    private String getTextFromFile(String fileName) throws FileNotFoundException, URISyntaxException, IOException
    {
      BufferedReader in = new BufferedReader(new FileReader(new File(this.getClass().getClassLoader().getResource(fileName).toURI())));
      String str;
      StringBuffer sb = new StringBuffer();
      while ((str = in.readLine()) != null){
          
          sb.append(str);
      }
      in.close();
      return sb.toString();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileNotFoundException {
        try {
            processRequest(request, response);
        } catch (URISyntaxException ex) {
            Logger.getLogger(MockBrowseCalendarHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileNotFoundException {
        try {
            processRequest(request, response);
        } catch (URISyntaxException ex) {
            Logger.getLogger(MockBrowseCalendarHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
